package github.flandre.examplemod.common.tileentity;

import github.flandre.examplemod.common.container.ExampleGuiContainer;
import github.flandre.examplemod.core.init.ItemInit;
import github.flandre.examplemod.core.init.TileEntityInit;
import github.flandre.examplemod.examplemod;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.IIntArray;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/*
 * ExampleGuiBlock 机器方块本身
 * ExampleGuiTileEntity 机器的实体
 * ExampleGuiContainer 实体的界面
 * DisplayCaseScreen 客户端渲染贴图
 * TileEntityInit 初始化
 * ContainerInit 初始化`
 * */

public class ExampleGuiTileEntity extends TileEntity  implements ITickableTileEntity , INamedContainerProvider {
    private static final int SLOTS = 3;//槽位
    public int compressorProgress = 0; //进度条

    public final IIntArray data = new IIntArray() {
        @Override
        public int get(int index) {
            return ExampleGuiTileEntity.this.compressorProgress;
        }

        @Override
        public void set(int index, int value) {
            ExampleGuiTileEntity.this.compressorProgress = value;
        }

        @Override
        public int size() {
            return 1;
        }
    };

    public boolean recipeFinish = false;

    //添加物品槽
    private final ItemStackHandler UP = new ItemStackHandler(1)
    {
        @Override
        protected void onContentsChanged(int slot) {
            ExampleGuiTileEntity.this.markDirty(); //保存数据
        }
    };
    private final ItemStackHandler SIDE = new ItemStackHandler(1)
    {
        @Override
        protected void onContentsChanged(int slot) {
            ExampleGuiTileEntity.this.markDirty(); //保存数据
        }
    };
    private final ItemStackHandler DOWN = new ItemStackHandler(1)
    {
        @Override
        protected void onContentsChanged(int slot) {
            ExampleGuiTileEntity.this.markDirty(); //保存数据
        }
    };

    //让实体具备交互的功能
    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        //和forge的物品进行对比
        Capability<IItemHandler> itemHandlerCapability = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
        if(cap.equals(itemHandlerCapability))
        {
            //方块的方向和交互口的方向是否一致
            if(Direction.UP.equals(side))
            {
                return LazyOptional.of(()->this.UP).cast();
            }
            if(Direction.DOWN.equals(side))
            {
                return LazyOptional.of(()->this.DOWN).cast();
            }
            if(Direction.NORTH.equals(side) || Direction.SOUTH.equals(side))
            {
                return LazyOptional.of(()->this.SIDE).cast();
            }
        }
        return super.getCapability(cap, side);
    }



    //保存序列化
    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        this.DOWN.deserializeNBT(nbt.getCompound("DOWN"));
        this.SIDE.deserializeNBT(nbt.getCompound("SIDE"));
        this.UP.deserializeNBT(nbt.getCompound("UP"));
        super.read(state, nbt);
    }
    @Override
    public CompoundNBT write(CompoundNBT compound) {
        compound.put("DOWN",this.DOWN.serializeNBT());
        compound.put("SIDE",this.SIDE.serializeNBT());
        compound.put("UP",this.UP.serializeNBT());
        return super.write(compound);
    }


    public ExampleGuiTileEntity(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }
    public ExampleGuiTileEntity()
    {
        this(TileEntityInit.ENTITY_TYPE_REGISTRY_GUI.get());
    }

    //每tick都会执行的代码
    @Override
    public void tick() {
        Item dirt = Items.DIRT;
        Item snowball = ItemInit.INFINITE_SNOWBALL.get();
        Item diamond = Items.DIAMOND;
        //获取从SIDE槽位中第0个槽位 数量1 不减少 和泥土进行比较
        boolean dirtEquals = dirt.equals(this.SIDE.extractItem(0, 1, true).getItem());
        boolean equals = snowball.equals(this.UP.extractItem(0, 1, true).getItem());
        if(dirtEquals && equals)
        {
            this.compressorProgress++;
            //每20tik减少一个物品
            if(compressorProgress%20==0 && !recipeFinish)
            {
                this.UP.extractItem(0,1,false);
            }
            //完成制作时
            if(compressorProgress>=240)
            {
                //判断是否可以在DOWN操插入一个钻石
                boolean equals1 = this.DOWN.insertItem(0, new ItemStack(diamond), true).isEmpty();
                if(equals1)
                {
                    this.DOWN.insertItem(0,new ItemStack(diamond),false);
                    this.compressorProgress = 0;
                    this.SIDE.extractItem(0,1,false);
                    recipeFinish = false;
                }
                else
                {
                    compressorProgress-=1;
                    recipeFinish = true;
                }
            }
            else
            {
                //保存数据
                markDirty();
            }
        }
        else
        {
            compressorProgress = 0;
            markDirty();
        }

    }

    //界面模组
    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container."+ examplemod.MODID+".example_gui_block");
    }

    //创建界面的
    @Nullable
    @Override
    public Container createMenu(int id_, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new ExampleGuiContainer(id_,playerInventory,this,data);
    }
}






